﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;

public class HFDownloadController : MonoBehaviour
{
	public string[] urls = new string[] {
		"http://192.168.43.78:8080/tower.Dispose.lua",
		"http://192.168.43.78:8080/tower.lua"
	};

	public IEnumerator Download(string url) {
		UnityWebRequest request = UnityWebRequest.Get(url);
		request.SendWebRequest();
		if (request.isNetworkError || request.isHttpError) {
			Debug.Log("网络错误");
			yield break;
		}
		while (!request.isDone) {

			HFUIController.Instance.SetPropress(request.downloadProgress);

			yield return null;
		}

		if (request.isDone) {
			HFUIController.Instance.SetPropress(1);
			string path = Application.persistentDataPath + "/" + Path.GetFileName(url);
			using (FileStream fs = new FileStream(path, FileMode.Create)) {
				byte[] bytes = request.downloadHandler.data;
				fs.Write(bytes, 0, bytes.Length);
			}
		}
	}


	// Start is called before the first frame update
	void Start() {
		foreach (string url in urls) {
			StartCoroutine(Download(url));

		}
	}

}
